<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加权限按钮 -->
      <div style="padding: 10px">
        <el-button type="primary" size="mini" @click="show([1,0])">添加权限</el-button>
      </div>
      <!-- 权限数据表格 -->
      <el-table :data="permissionList" row-key="id" default-expand-all>
        <el-table-column label="名称" prop="name" />
        <el-table-column
          label="标识"
          prop="code"
        />
        <el-table-column
          label="描述"
          prop="description"
        />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="show([2,row.id])">添加</el-button>
            <el-button type="text" size="mini" @click="show([null,row.id])">编辑</el-button>
            <el-popconfirm
              title="确定删除吗？"
              @onConfirm="deleteId(row.id)"
            >
              <el-button slot="reference" type="text" size="mini" style="margin-left: 10px;">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table>
      <ShowDialog ref="showD" :dialog-visible.sync="dialogVisible" :type="type" @remove="remove" />
    </div>
  </div>
</template>
<script>
import { getPermissionList, deletePermission } from '@/api/permission'
import { transList } from '@/utils/index'
import ShowDialog from '@/views/permission/components/show-dialog.vue'
export default {
  name: 'Permission',
  components: {
    ShowDialog
  },
  data() {
    return {
      permissionList: [],
      dialogVisible: false,
      type: null
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.permissionList = transList(await getPermissionList())
    },
    show(type) {
      this.type = type

      this.$nextTick(() => {
        if (this.type[0] === null) {
          this.$refs.showD.singleData()
        }
      })
      this.dialogVisible = true
    },
    remove() {
      this.getPermissionList()
    },
    async deleteId(id) {
      await deletePermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    }
  }

}
</script>
